package com.platform.usercenter.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.platform.common.core.exception.ServiceException;
import com.platform.common.core.utils.DateUtils;
import com.platform.common.security.utils.SecurityUtils;
import com.platform.constants.SysUserConstants;
import com.platform.usercenter.domain.SysAppPermission;
import com.platform.usercenter.domain.dto.SysAppSceneDTO;
import com.platform.usercenter.mapper.SysAppPermissionMapper;
import com.platform.usercenter.utils.TreeAdapterUtil;
import org.springframework.stereotype.Service;
import com.platform.usercenter.mapper.SysAppSceneMapper;
import com.platform.usercenter.domain.SysAppScene;
import com.platform.usercenter.service.ISysAppSceneService;

import javax.annotation.Resource;

/**
 * 组织上下游关系Service业务层处理
 *
 * @author platform
 * @date 2024-10-28
 */
@Service
public class SysAppSceneServiceImpl implements ISysAppSceneService {
    @Resource
    private SysAppSceneMapper sysAppSceneMapper;

    @Resource
    private SysAppPermissionMapper sysAppPermissionMapper;

    /**
     * 查询组织上下游关系
     *
     * @param appSceneId 组织上下游关系主键
     * @return 组织上下游关系
     */
    @Override
    public SysAppScene selectSysAppSceneByAppId(Long appSceneId) {
        return sysAppSceneMapper.selectSysAppSceneByAppId(appSceneId);
    }

    @Override
    public SysAppScene selectSysAppSceneByPath(Long appSceneId) {
        return sysAppSceneMapper.selectSysAppSceneByPath(appSceneId);
    }

    /**
     * 查询组织上下游关系列表
     *
     * @param sysAppScene 组织上下游关系
     * @return 组织上下游关系
     */
    @Override
    public List<SysAppScene> selectSysAppSceneList(SysAppScene sysAppScene) {
        return sysAppSceneMapper.selectSysAppSceneList(sysAppScene);
    }

    @Override
    public List<SysAppScene> listTree(SysAppScene sysAppScene) {
        List<SysAppScene> appSceneList = sysAppSceneMapper.selectSysAppSceneList(sysAppScene);
        if (CollectionUtil.isNotEmpty(appSceneList)) {
            return TreeAdapterUtil.buildTree(appSceneList,SysAppScene.class,null);
        } else {
            return null;
        }
    }


    public static List<SysAppScene> buildTree(List<SysAppScene> appScenes) {
        List<SysAppScene> tree = new ArrayList<>();
        for (SysAppScene scene : appScenes) {
            if (scene.getParentId() == 0) {
                tree.add(buildSubTree(scene, appScenes));
            }
        }
        return tree;
    }

    private static SysAppScene buildSubTree(SysAppScene root, List<SysAppScene> appScenes) {
        for (SysAppScene scene : appScenes) {
            if (scene.getParentId() == root.getAppSceneId()) {
                root.getChildren().add(buildSubTree(scene, appScenes));
            }
        }
        return root;
    }


    @Override
    public int bindAppKey(SysAppSceneDTO dto) {
        SysAppPermission permission = new SysAppPermission();
        permission.setAppSceneId(dto.getAppSceneId());
        permission.setCode(dto.getCode());
        permission.setIsDelete(SysUserConstants.N);
        List<SysAppPermission> permissionList=sysAppPermissionMapper.selectSysAppPermissionList(permission);
        if(CollectionUtil.isNotEmpty(permissionList)){
            throw new ServiceException("该应用下key值已存在");
        }
        SysAppPermission appPermission = new SysAppPermission();
        appPermission.setCreateBy(SecurityUtils.getUsername());
        appPermission.setCreateTime(DateUtils.getNowDate());
        appPermission.setIsDelete(SysUserConstants.N);
        appPermission.setAppSceneId(dto.getAppSceneId());
        appPermission.setCode(dto.getCode());
        appPermission.setName(dto.getName());
        appPermission.setIsMust(dto.getIsMust());

        return sysAppPermissionMapper.insertSysAppPermission(appPermission);
    }

    /**
     * 新增组织上下游关系
     *
     * @param sysAppScene 组织上下游关系
     * @return 结果
     */
    @Override
    public int insertSysAppScene(SysAppScene sysAppScene) {
        if(ObjectUtil.isEmpty(sysAppScene.getParentId())){
            sysAppScene.setParentId(SysUserConstants.PARENT_0);
        }
        SysAppScene query = new SysAppScene();
        query.setIsDelete(SysUserConstants.N);
        query.setParentId(sysAppScene.getParentId());
        query.setSceneName(sysAppScene.getSceneName());
        List<SysAppScene> sysAppSceneList = sysAppSceneMapper.selectSysAppSceneListCheck(query);
        if (CollectionUtil.isNotEmpty(sysAppSceneList)) {
            throw new ServiceException("应用场景已存在：" + sysAppScene.getSceneName());
        }
        sysAppScene.setCreateTime(DateUtils.getNowDate());
        sysAppScene.setCreateBy(SecurityUtils.getUsername());
        sysAppScene.setIsDelete(SysUserConstants.N);
        return sysAppSceneMapper.insertSysAppScene(sysAppScene);
    }

    /**
     * 修改组织上下游关系
     *
     * @param sysAppScene 组织上下游关系
     * @return 结果
     */
    @Override
    public int updateSysAppScene(SysAppScene sysAppScene) {
        if(ObjectUtil.isEmpty(sysAppScene.getParentId())){
            sysAppScene.setParentId(SysUserConstants.PARENT_0);
        }
        SysAppScene query = new SysAppScene();
        query.setIsDelete(SysUserConstants.N);
        query.setParentId(sysAppScene.getParentId());
        query.setSceneName(sysAppScene.getSceneName());
        List<SysAppScene> sysAppSceneList = sysAppSceneMapper.selectSysAppSceneListCheck(query);
        if (CollectionUtil.isNotEmpty(sysAppSceneList) && !sysAppSceneList.get(0).getAppSceneId().equals(sysAppScene.getAppSceneId())) {
            throw new ServiceException("应用场景已存在：" + sysAppScene.getSceneName());
        }
        sysAppScene.setUpdateBy(SecurityUtils.getUsername());
        sysAppScene.setUpdateTime(DateUtils.getNowDate());
        return sysAppSceneMapper.updateSysAppScene(sysAppScene);
    }

    /**
     * 批量删除组织上下游关系
     *
     * @param appSceneIds 需要删除的组织上下游关系主键
     * @return 结果
     */
    @Override
    public int deleteSysAppSceneByAppIds(Long[] appSceneIds) {
        int rows = 0;
        if (ObjectUtil.isNotEmpty(appSceneIds)) {
            for (Long id : appSceneIds) {
                SysAppScene appScene = new SysAppScene();
                appScene.setAppSceneId(id);
                appScene.setDeleteBy(SecurityUtils.getUsername());
                appScene.setDeleteTime(DateUtils.getNowDate());
                appScene.setIsDelete(SysUserConstants.Y);
                sysAppSceneMapper.updateSysAppScene(appScene);
                rows++;
            }
        }
        return rows;
    }

    /**
     * 删除组织上下游关系信息
     *
     * @param appSceneId 组织上下游关系主键
     * @return 结果
     */
    @Override
    public int deleteSysAppSceneByAppId(Long appSceneId) {
        return sysAppSceneMapper.deleteSysAppSceneByAppId(appSceneId);
    }
}
